Skip to content

Conversation

Tatsunori-Morita
Copy link
Contributor

Summary

This pull request addresses issue #67 by adding a feature that allows the year picker to be shown first.

Implementation details:

  • For DatePickerDialog: Used an OnShowListener so that when the dialog is displayed, the year view button is retrieved via the date_picker_header_year ID and programmatically clicked to show the year selector.
  • For MaterialDatePicker: Retrieved the view responsible for displaying the year within the picker’s view group and programmatically triggered it to switch to the year selection view.

Test Plan

Tested using an Android emulator.

  • Pixel 6 (Android 12) DatePickerDialog
pixcel6-android12-default_WStd3B14.mp4
  • Pixel 6 (Android 12) MaterialDatePicker
pixcel6-android12-material_X3kctEEi.mp4
  • Pixel 7 (Android 14) DatePickerDialog
pixcel7-android14-default_D6ZmfYhW.mp4
  • Pixel 7 (Android 14) MaterialDatePicker
pixcel7-android14-material_qUr9dRH2.mp4
  • Nexus 5X (Android 7) DatePickerDialog
nexus5x-android7-default_bIXOxaYC.mp4
  • Nexus 5X (Android 7) MaterialDatePicker
nexus5x-android7-material_W1Lry5gS.mp4

What's required for testing (prerequisites)?

What are the steps to reproduce (after prerequisites)?

Compatibility

OS Implemented
iOS
Android

Checklist

  • I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I updated the typed files (TS and Flow)
  • I added a sample use of the API in the example project (example/App.js)
  • I have added automated tests, either in JS or e2e tests, as applicable

Copy link
Member

@vonovak vonovak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!
Generally this seem okay. I added some comments, please take a look.

Also, I'm not quite sold on the showYearPickerFirst prop name. I'm thinking maybe startOnYearSelection, or initialSelection 'year' | 'date'... What would you suggest?

Also, tests need to pass. Maybe just re-running them will help.

Thank you!

Comment on lines 122 to 139
activity?.let { lifecycleOwner ->
datePicker!!.viewLifecycleOwnerLiveData.observe(lifecycleOwner) { owner ->
if (owner != null) {
datePicker?.requireDialog()?.window?.decorView?.post {
val root = datePicker!!.dialog?.window?.decorView ?: return@post

val yearText = initialDate.year().toString()
val hit = findViewBy(root) { v ->
v is TextView && v.isShown && v.isClickable && v.text?.toString()?.contains(yearText) == true
}
if (hit != null) {
hit.performClick()
return@post
}
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems rather hacky... but okay. Let's say we use this.

But before we do, please

  • no force unwrap in this code
  • change if (owner != null) to if (owner == null) and an early return
  • we're adding an observer. Should we clean it up also?

DatePicker datePicker = datePickerDialog.getDatePicker();

int yearId = Resources.getSystem().getIdentifier("date_picker_header_year", "id", "android");
View yearView = datePicker.findViewById(yearId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's please add an early return if yearId == 0. It probably isn't needed but let's be on the safer side of things. Android is a mess so you never know.

boolean showYearPickerFirst = args.getBoolean(RNConstants.ARG_SHOW_YEAR_PICKER_FIRST);
dialog.setOnShowListener(
combine(
openYearDialog(dialog, canOpenYearDialog, showYearPickerFirst),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
openYearDialog(dialog, canOpenYearDialog, showYearPickerFirst),
openYearDialog(dialog, canOpenYearDialog && showYearPickerFirst),

Fewer booleans is better than more

@Tatsunori-Morita
Copy link
Contributor Author

@vonovak
Thanks a lot for the review and the helpful comments! 🙏

I understand you’re not quite sold on the showYearPickerFirst prop name — could you clarify a bit more about what specifically doesn’t feel right with it?
Is it about potential future extensibility (for example, starting on week and so on)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants